<?php
class hrd_detail_jadwal_model extends MY_Model {
	var $nama_tabel = 'hrd_detail_jadwal';
	
	function GetJSON($idJadwal) {
		$ls_sql	= "	SELECT	a.*, b.jam_mulai, b.jam_selesai
				FROM	".$this->nama_tabel." a
				LEFT JOIN hrd_shift b ON a.id_shift_fk = b.id_shift_pk
				WHERE 	id_jadwal_fk = '".$idJadwal."'
				ORDER BY a.urutan";

		$row = $this->db->query($ls_sql)->result_array();
		$data = array('rows' => $row, 
					  'total' => count($row),
					  'page' => 1);
		
		return json_encode($data);
	}

	function GetImportJSON()
	{
		$ls_sql	= "	SELECT	a.id_jadwal_fk, TO_CHAR(a.tgl_jadwal,'DD/MM/YYYY') tgl_jadwal
					FROM	hrd_detail_jadwal a 
					WHERE tgl_jadwal BETWEEN 
							TO_DATE('".date_format3(post('date_from'), 'YYYY-MM-DD')."','YYYY-MM-DD') AND 
							TO_DATE('".date_format3(post('date_to'), 'YYYY-MM-DD')."','YYYY-MM-DD') AND
							(
								UPPER(a.id_jadwal_fk) LIKE '%".strtoupper(post('keyword'))."%' 
							)
					ORDER BY ".(post('sort')=='' ? 'tgl_jadwal' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
		// echo $ls_sql;	
		return json_encode( $this->paging($ls_sql) );
	}

	function Add($arr_data){
		$this->db->insert($this->nama_tabel, $arr_data);
	}

	function Update($arr_data, $arr_rule){
		$this->db->update($this->nama_tabel, $arr_data, $arr_rule);
	}

	function DeleteSingle($arr_rule) {
		try {
			$this->db->delete($this->nama_tabel, array('id_detail_jadwal_pk' => $arr_rule['id_detail_jadwal_pk']));
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}

	function DeletePerJadwal($idJadwal){
		$this->db->where('id_jadwal_fk', $idJadwal);
		$this->db->delete($this->nama_tabel);
	}

}
?>